package hu.szyrt.bug;

import java.util.ArrayList;
import hu.szyrt.bug.adapters.PlacesListAdapter;
import hu.szyrt.bug.db.DBHandler;
import hu.szyrt.bug.models.PlaceItemBase;
import hu.szyrt.bug.models.Tour;
import hu.szyrt.bug.utils.Assets;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class ToursActivity extends Activity {
	
	Assets assets;
	DBHandler dbHandler;
	
	private final static int DISPLAY_DEFAULT 		= 0,							 
							 DISPLAY_PLACES_LIST	= 1;
	
	// ViewFlipper --------------------
	ViewFlipper viewFlipper;	
	Animation 	animationForwardIN, animationForwardOUT,
	  			animationBackIN, animationBackOUT;

	// SubCategory --------------------
	View subCategoryView;
	ListView subCategoryListView;
	PlacesListAdapter subCategoryAdapter;
	
	// Places ------------------------- 
	View placesView;
	ListView placesListView;
	PlacesListAdapter placesAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tours);
		
		this.assets = new Assets(this);
		this.dbHandler = new DBHandler(this);
		
		// ViewFlipper ---------------------
		viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
		
		// Animációk --------------------------------------
		// Vizszintes: Jobbra, balra
		animationForwardIN = AnimationUtils.loadAnimation(this, R.anim.push_left_in);	// új belépő
		animationForwardOUT = AnimationUtils.loadAnimation(this, R.anim.push_left_out); // régi kilépő
		animationBackIN = AnimationUtils.loadAnimation(this, R.anim.push_right_in);     // új belépő
		animationBackOUT = AnimationUtils.loadAnimation(this, R.anim.push_right_out);	// régi kilépő	
		
		
		
		// SubCategoies --------------------	
		subCategoryView = findViewById(R.id.subCategoryList);
		subCategoryListView = (ListView)subCategoryView.findViewById(R.id.listView);
		subCategoryListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> a, View v, int i, long l) {
				PlaceItemBase item = (PlaceItemBase)subCategoryListView.getItemAtPosition(i);
				if(item != null){
					if(item instanceof Tour)
						loadPlaces((Tour)item);
					else 
						showPlace(item.getId());
				}
			}
		});
		Button backButton = (Button)subCategoryView.findViewById(R.id.backButton);
			   backButton.setCompoundDrawables(null, null, null, null);
			   backButton.setPadding(10, 0, 0, 0);
		   	   backButton.setText(R.string.tours);
		 
		EditText searchEditText = (EditText)subCategoryView.findViewById(R.id.search);
				 searchEditText.setVisibility(View.GONE);
		
		ArrayList<PlaceItemBase> subCategories = new ArrayList<PlaceItemBase>();
		subCategories.addAll(dbHandler.loadTours());
		subCategories.addAll(dbHandler.loadTourPlaces(null));
		subCategoryAdapter = new PlacesListAdapter(assets, subCategories);
		subCategoryListView.setAdapter(subCategoryAdapter);
		
		
		
		// Places List ---------------------
		placesView = findViewById(R.id.placesList);
		placesListView = (ListView)placesView.findViewById(R.id.listView);
		placesListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> a, View v, int i, long l) {
				PlaceItemBase item = (PlaceItemBase)placesListView.getItemAtPosition(i);
				if(item != null) showPlace(item.getId());				
			}
		});
	}
	
	
	private void loadPlaces(Tour tour){		
		if(placesAdapter == null){
			placesAdapter = new PlacesListAdapter(assets, dbHandler.loadTourPlaces(tour));
			placesListView.setAdapter(placesAdapter);
		}else{
			placesAdapter.changeList(dbHandler.loadTourPlaces(tour));
		}
		
		Button backButton = (Button)placesView.findViewById(R.id.backButton);
			   backButton.setText(tour.getName());
			   backButton.setOnClickListener(backButtonClickListener);
			   
		EditText searchEditText = (EditText)placesView.findViewById(R.id.search);
				 searchEditText.setVisibility(View.GONE);
			   
	
		viewFlipper.setInAnimation(animationForwardIN);
		viewFlipper.setOutAnimation(animationForwardOUT);
		viewFlipper.setDisplayedChild(DISPLAY_PLACES_LIST);
	}
	
	private void hidePlaces(){
		viewFlipper.setInAnimation(animationBackIN);
		viewFlipper.setOutAnimation(animationBackOUT);
		viewFlipper.setDisplayedChild(DISPLAY_DEFAULT);
	}
	
	
	
	private void showPlace(int id){
		PlaceActivity.showPlaceBy(this, PlaceActivity.PLACE_ID, id);
	}
	
	// Back
	public boolean showPrev(){	// lapozás
		if(viewFlipper != null && viewFlipper.getDisplayedChild() > DISPLAY_DEFAULT){
			switch (viewFlipper.getDisplayedChild()) {				
				case DISPLAY_PLACES_LIST: hidePlaces(); return true;
				default: return false;
			}			
		}
		return false;
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			return showPrev();
		}else return super.onKeyUp(keyCode, event);
	}
	
	private OnClickListener backButtonClickListener = new OnClickListener() {
		public void onClick(View v) {
			showPrev();
		}
	};
	
	
	
}
